package cn.maihe.elg.operation.exception;

/**
 * @author JHWei
 */
public class SystemRuntimeException extends RuntimeException {
    /**
     * 错误码
     */
    private ErrorCode code;
    /**
     * 嵌套异常
     */
    private Exception exception;
    /**
     * 错误信息参数
     */
    private String[] args;

    public SystemRuntimeException() {
    }

    public SystemRuntimeException(ErrorCode code, String... args) {
        this.code = code;
        this.args = args;
    }

    public SystemRuntimeException(ErrorCode code, Exception exception, String... args) {
        super(args != null ? args[0] : exception.getMessage(), exception);
        this.code = code;
        this.args = args;
        this.exception = exception;
    }

    public SystemRuntimeException(ErrorCode code, Exception exception) {
        this.code = code;
        this.exception = exception;
    }

    public String[] getArgs() {
        return args;
    }

    public void setException(Exception exception) {
        this.exception = exception;
    }

    public ErrorCode getCode() {
        return code;
    }

    public Exception getException() {
        return exception;
    }

    public static SystemRuntimeException of(ErrorCode code) {
        return new SystemRuntimeException(code);
    }

    public static SystemRuntimeException error(String errorMsg, Exception exception) {
        return new SystemRuntimeException(ErrorCode.ERROR, exception, errorMsg);
    }

    public static SystemRuntimeException error(String errorMsg) {
        return new SystemRuntimeException(ErrorCode.ERROR, errorMsg);
    }

    public SystemRuntimeException args(String... args) {
        this.args = args;
        return this;
    }

    public SystemRuntimeException exception(Exception e) {
        this.exception = e;
        return this;
    }
}
